package com.lightningtrade.security;

import com.lightningtrade.domain.SysUser;
import com.lightningtrade.mapper.SysLoginMapper;
import com.lightningtrade.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysLoginMapper sysLoginMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        // 查询用户信息
        SysUser user = sysLoginMapper.getUserByUsername(username);

        //如果没有查询到用户就抛出异常
        if(Objects.isNull(user)){
            System.out.println("UserDetailsServiceImpl --- 查询用户不存在");
            throw new UsernameNotFoundException("用户不存在");
        }

        System.out.println("查询到用户:\n\t"+user.getUsername() + "\n");

        // 把数据封装成UserDetails返回
        return new UserDetails(user);
    }
}
